package leetcode.medium.limit50;

import java.util.*;

/**
 * @author yaoyao1.liu@zhaopin.com.cn
 * @since 2022-11-09 15:28
 **/
public class No49 {
    public static void main(String[] args) {
        No49 no49 = new No49();
        List<List<String>> lists = no49.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
        System.out.println(lists);
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String string = Arrays.toString(chars);
            List<String> list = map.getOrDefault(string, new ArrayList<>());
            list.add(str);
            map.put(string, list);
        }
        ArrayList<List<String>> objects = new ArrayList<>();
        map.forEach((key, value) -> {
            objects.add(value);
        });
        return objects;
    }

}
